<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/10/22
 * Time: 16:01
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\common\business;


use app\common\lib\Status;
use app\common\validate\Collection as ValObj;
use app\common\model\mysql\Collection as ModelObj;
use think\Exception;
use think\facade\Log;

class Collection extends Base
{
    /**
     * @var ValObj
     */
    protected ValObj $valObj;
    /**
     * @var ModelObj
     */
    protected ModelObj $modelObj;

    /**
     *
     */
    public function __construct()
    {
        $this->valObj = new ValObj();
        $this->modelObj = new ModelObj();
    }

    /**
     * 收藏处理
     * @param int $theatre_id
     * @param string $openid
     * @return false|string
     * @throws Exception
     */
    public function setCollection(int $theatre_id = 0, string $openid = "")
    {
        //查询
        try {
            $theatre = (new \app\common\model\mysql\Theatre())->getBaseById($theatre_id);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        if (!$theatre) {
            throw new Exception("场次不存在");
        }
        try {
            $collection = $this->getCollectionByOpenidId($theatre_id, $openid);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        $msg = "收藏成功";
        try {
            if (!$collection) {
                $result = $this->modelObj->insertAdd([
                    "theatre_id" => $theatre_id,
                    "openid" => $openid,
                    "start_time" => strtotime($theatre['start_time'])
                ]);
            } else {
                $result = $this->modelObj->updateByData($collection['id'], [
                    "status" => Status::mysql_error,
                    "start_time" => strtotime($theatre['start_time']),
                    "update_time" => time()
                ]);
                $msg = "取消成功";
            }
        } catch (\Exception $e) {
            throw new Exception("操作失败");
        }
        if ($result) {
            return $msg;
        } else {
            return false;
        }
    }

    /**
     * @param int $theatre_id
     * @param string $openid
     * @return ModelObj|array|\think\Model|null
     * @throws Exception
     */
    public function getCollectionByOpenidId(int $theatre_id = 0, string $openid = "")
    {
        try {
            $collection = $this->modelObj->getCollectionByOpenid($theatre_id, $openid);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $collection;
    }

    /**
     * @param string $openid
     * @param string $field
     * @return array
     * @throws Exception
     */
    public function getCollectionByList(string $openid = '', string $field = '*')
    {
        try {
            $collection = $this->modelObj->getCollectionByList($openid, $field);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $collection;
    }

    /**
     * @param int $id
     * @return bool
     * @throws Exception
     */
    public function getCollection(int $id = 0){
        try{
            $theatre = (new \app\common\model\mysql\Theatre())->getBaseById($id);
        }catch (\Exception $e){
            throw new Exception("查询失败");
        }
        if(!$theatre){
            throw new Exception("订单错误");
        }
        if(strtotime($theatre['start_time']) - 120 < time()){
            throw new Exception("订单开场时间不足");
        }
        return true;
    }

}